package com.weijian.controller;

import com.weijian.common.ApiResponse;
import com.weijian.dal.entity.User;
import com.weijian.dal.dto.user.UserLoginRequest;
import com.weijian.dal.dto.user.UserRegisterRequest;
import com.weijian.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author weijian
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户注册、登录、登出等相关接口")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "新用户注册接口，需要用户名、邮箱和密码")
    @PostMapping("/register")
    public ApiResponse<User> register(@Validated @RequestBody UserRegisterRequest registerRequest) {
        log.info("用户注册请求 - 用户名: {}, 邮箱: {}",
                registerRequest.getUsername(), registerRequest.getEmail());
        return userService.register(registerRequest);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "用户登录接口，支持用户名或邮箱登录")
    @PostMapping("/login")
    public ApiResponse<User> login(@Validated @RequestBody UserLoginRequest loginRequest,
                                   HttpServletResponse response) {
        log.info("用户登录请求 - 用户名/邮箱: {}", loginRequest.getUsernameOrEmail());
        return userService.login(loginRequest, response);
    }

    /**
     * 用户登出
     */
    @Operation(summary = "用户登出", description = "用户登出接口，清除会话信息")
    @PostMapping("/logout")
    public ApiResponse<Void> logout(HttpServletRequest request, HttpServletResponse response) {
        log.info("用户登出请求");
        return userService.logout(request, response);
    }

    /**
     * 获取当前登录用户信息
     */
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/current")
    public ApiResponse<User> getCurrentUser(HttpServletRequest request) {
        log.info("获取当前用户信息请求");
        try {
            return userService.getCurrentUser(request);
        } catch (Exception e) {
            log.error("发生错误:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 根据ID获取用户信息
     */
    @Operation(summary = "根据ID获取用户信息", description = "根据用户ID获取用户详细信息")
    @GetMapping("/{userId}")
    public ApiResponse<User> getUserById(
            @Parameter(description = "用户ID", required = true, example = "1")
            @PathVariable Long userId) {
        log.info("获取用户信息请求 - 用户ID: {}", userId);
        return userService.getUserById(userId);
    }

    /**
     * 检查登录状态
     */
    @Operation(summary = "检查登录状态", description = "检查当前用户是否已登录")
    @GetMapping("/check-login")
    public ApiResponse<Boolean> checkLogin(HttpServletRequest request) {
        boolean loggedIn = userService.isLoggedIn(request);
        return ApiResponse.success(loggedIn);
    }

    /**
     * 注销用户账号（永久删除）
     */
    @Operation(summary = "注销用户账号", description = "永久删除当前用户的账号，需要验证密码")
    @PostMapping("/delete-account")
    public ApiResponse<Void> deleteAccount(@RequestParam String password,
                                           HttpServletRequest request) {
        log.info("用户注销账号请求");
        return userService.deleteAccount(password, request);
    }
}